package cn.laixueit.service.impl;

import cn.laixueit.mapper.TeacherMapper;
import cn.laixueit.pojo.Teacher;
import cn.laixueit.pojo.TeacherExample;
import cn.laixueit.service.TeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = RuntimeException.class)
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public int save(Teacher teacher) {
        return teacherMapper.insertSelective(teacher);
    }

    @Override
    public PageInfo<Teacher> findall(Integer startpage, Integer pagesize, Teacher teacher) {
        TeacherExample example = getCondtion(teacher);
        PageHelper.startPage(startpage,pagesize);
        List<Teacher> teacherList = teacherMapper.selectByExample(example);
        PageInfo<Teacher> pageInfo = new PageInfo<>(teacherList);
        return pageInfo;
    }

    @Override
    public Teacher findbyid(Integer id) {
        return teacherMapper.selectByPrimaryKey(id);
    }

    private TeacherExample getCondtion(Teacher teacher) {
        TeacherExample example = new TeacherExample();
        example.setOrderByClause("teacheryear desc");
        if(teacher!=null){
            TeacherExample.Criteria criteria = example.createCriteria();
            if(teacher.getName()!=null && teacher.getName().trim()!=""){
                criteria.andNameLike("%"+teacher.getName()+"%");
            }
            if(teacher.getGender()!=null){
                criteria.andGenderEqualTo(teacher.getGender());
            }
        }
        return example;
    }
}
